﻿#region Using Statements
using System;
using System.IO;
using System.Xml;
#endregion

namespace StarFox
{
    #region Graphics Configuration

    /// <summary>
    /// Represents one possible graphics configuration for the game.
    /// </summary>
    public class GraphicsConfig
    {
        #region Properties

        /// <summary>
        /// Gets the width of the display, in pixels.
        /// </summary>
        public int DisplayWidth
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the height of the display, in pixels.
        /// </summary>
        public int DisplayHeight
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the mode of the display.
        /// <para>True for fullscreen mode. False for windowed mode.</para>
        /// </summary>
        public bool DisplayMode
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the antialiasing mode.
        /// <para>True for antialiasing active. False for antialiasing inactive.</para>
        /// </summary>
        public bool AntiAliasing
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the postprocessing mode.
        /// <para>True for postprocessing active. False for postprocessing inactive.</para>
        /// </summary>
        public bool PostProcessing
        {
            get;
            private set;
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="displayWidth">Width of the display, in pixels.</param>
        /// <param name="displayHeight">Height of the display, in pixels.</param>
        /// <param name="displayMode">Mode of the display.</param>
        /// <param name="antialiasing">Antialiasing mode.</param>
        /// <param name="postprocessing">Postprocessing mode.</param>
        public GraphicsConfig(int displayWidth, int displayHeight, bool displayMode, bool antialiasing, bool postprocessing)
        {
            DisplayWidth = displayWidth;
            DisplayHeight = displayHeight;
            DisplayMode = displayMode;
            AntiAliasing = antialiasing;
            PostProcessing = postprocessing;
        }

        #endregion
    }

    #endregion

    static class StarFoxMain
    {
        #region Display Settings - Parameters

        static int displayWidth;
        static int displayHeight;
        static bool displayMode;
        static bool antiAliasing;
        static bool postProcessing;

        #endregion

        #region Display Settings - Load

        static readonly string settingsFile = "StarFoxSettings.xml";

        // display settings loaded correctly
        static bool isLoadOk;

        private static void LoadSettings()
        {
            isLoadOk = false;

            if (!File.Exists(settingsFile))
                return;

            using (XmlReader reader = XmlReader.Create(settingsFile))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "settings")
                    {
                        isLoadOk = true;
                        break;
                    }
                }

                if (!isLoadOk)
                    return;
                else
                    isLoadOk = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "game")
                    {
                        isLoadOk = true;
                        break;
                    }
                }

                if (!isLoadOk)
                    return;
                else
                    isLoadOk = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "width")
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Text)
                        {
                            try
                            {
                                displayWidth = Convert.ToInt32(reader.Value);
                            }
                            catch
                            {
                                return;
                            }

                            isLoadOk = true;
                            break;
                        }
                    }
                }

                if (!isLoadOk)
                    return;
                else
                    isLoadOk = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "height")
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Text)
                        {
                            try
                            {
                                displayHeight = Convert.ToInt32(reader.Value);
                            }
                            catch
                            {
                                return;
                            }

                            isLoadOk = true;
                            break;
                        }
                    }
                }

                if (!isLoadOk)
                    return;
                else
                    isLoadOk = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "mode")
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Text)
                        {
                            try
                            {
                                displayMode = Convert.ToBoolean(reader.Value);
                            }
                            catch
                            {
                                return;
                            }

                            isLoadOk = true;
                            break;
                        }
                    }
                }

                if (!isLoadOk)
                    return;
                else
                    isLoadOk = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "antialiasing")
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Text)
                        {
                            try
                            {
                                antiAliasing = Convert.ToBoolean(reader.Value);
                            }
                            catch
                            {
                                return;
                            }

                            isLoadOk = true;
                            break;
                        }
                    }
                }

                if (!isLoadOk)
                    return;
                else
                    isLoadOk = false;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "postprocessing")
                    {
                        reader.Read();
                        if (reader.NodeType == XmlNodeType.Text)
                        {
                            try
                            {
                                postProcessing = Convert.ToBoolean(reader.Value);
                            }
                            catch
                            {
                                return;
                            }

                            isLoadOk = true;
                            break;
                        }
                    }
                }
            }
        }

        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // load settings from configuration file...
            // ... if settings are wrong, the game won't launch...
            // ... to fix them, use the launcher

            LoadSettings();

            if (!isLoadOk)
                return;

            GraphicsConfig graphicsConfig = new GraphicsConfig(displayWidth, displayHeight, displayMode, antiAliasing, postProcessing);

            using (StarFoxGame game = new StarFoxGame(graphicsConfig))
            {
                game.Run();
            }
        }
    }
}
